package com.nuoniu.sibanyun.service.crm.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.SysUser;
import com.nuoniu.sibanyun.entity.crm.ApprovalProcess;
import com.nuoniu.sibanyun.entity.crm.WithdrawalRecord;
import com.nuoniu.sibanyun.mapper.SysUserMapper;
import com.nuoniu.sibanyun.mapper.crm.ApprovalProcessMapper;
import com.nuoniu.sibanyun.mapper.crm.WithdrawalRecordMapper;
import com.nuoniu.sibanyun.service.crm.IApprovalService;
import com.nuoniu.sibanyun.service.crm.IWithdrawalRecordService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 提现记录 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-07-28
 */
@Service
public class WithdrawalRecordServiceImpl extends ServiceImpl<WithdrawalRecordMapper, WithdrawalRecord> implements IWithdrawalRecordService {

    @Autowired
    private WithdrawalRecordMapper withdrawalRecordMapper;
    @Autowired
    private IApprovalService approvalService;
    @Autowired
    private ApprovalProcessMapper processMapper;
    @Autowired
    private SysUserMapper userMapper;

    @Override
    public HttpResult withdrawalCommission(Integer amount) {
        UserInfo userInfo = UserThreadLocal.get();
        SysUser user = userMapper.selectById(userInfo.getUserId());
        if(StringUtils.isBlank(user.getBankName()) || StringUtils.isBlank(user.getBankAccount())) {
            throw new NuoNiuException("银行卡信息不能为空!");
        }

        List<WithdrawalRecord> commissionRecords = withdrawalRecordMapper.getListByStatus(userInfo.getUserId());
        if(commissionRecords.size() > 0) {
            throw new NuoNiuException("已有提现正在处理，暂时不能提现!");
        }

        if(user.getTotalCommission().subtract(user.getWithdrawalCommission()).compareTo(new BigDecimal(amount)) == -1) {
            throw new NuoNiuException("可提现余额不足");
        }

        List<ApprovalProcess> list = processMapper.getProcessByType(5, 1);
        if(list.size() == 0) {
            throw new NuoNiuException("系统异常，请联系客户!");
        }

        //提现处理
        WithdrawalRecord record = new WithdrawalRecord();
        record.setAmount(new BigDecimal(amount));
        record.setBankAccount(user.getBankAccount());
        record.setBankName(user.getBankName());
        record.setUserId(user.getId().intValue());
        record.setUserName(user.getRealName());
        withdrawalRecordMapper.insert(record);
        //提现审核流程
        ApprovalProcess approvalProcess = list.get(0);
        approvalService.startProcess(approvalProcess.getId(), record.getId(), "来自" + userInfo.getRealName() + "的提现审批",
                approvalProcess.getApprovalUserId(), approvalProcess.getProcessType(), approvalProcess.getApprovalMode(), list.size());

        user.setWithdrawalCommission(user.getWithdrawalCommission().add(new BigDecimal(amount)));
        userMapper.updateById(user);
        return HttpResult.ok();
    }

    @Override
    public Page<WithdrawalRecord> getWithdrawalRecord(Page pages, WithdrawalRecord record) {
        UserInfo userInfo = UserThreadLocal.get();
        WithdrawalRecord withdrawalRecord = new WithdrawalRecord();
        QueryWrapper<WithdrawalRecord> queryWrapper = new QueryWrapper<>(withdrawalRecord);
        queryWrapper.orderByDesc("create_date");
        if(userInfo.getUserId() != 1) {
            queryWrapper.eq("user_id", userInfo.getUserId());
        }

        if(record.getStatus() != null) {
            queryWrapper.eq("status", record.getStatus());
        }

        if(StringUtils.isNotBlank(record.getUserName())) {
            queryWrapper.like("user_name", record.getUserName());
        }
        return page(pages, queryWrapper);
    }
}
